/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mod8_Ex2;

/**
 *
 * @author Root
 */
public class CheckingAccount extends Account {

    private double overdrafProtecttion = 0;

    public CheckingAccount(double balance) {
        super(balance);
    }

    public double getOverdrafProtecttion() {
        return overdrafProtecttion;
    }

    public CheckingAccount(double balance, double protect) {
        super(balance);
        this.overdrafProtecttion = protect;
//        System.out.println("creating checking account with a " + balance + " balance and " + protect + " in overdraft protection");
    }

    @Override
    public String toString() {
        if (this.getOverdrafProtecttion() > 0) {
            return "CheckingAccount with a " + getBalance() + " balance and " + overdrafProtecttion + " overdraf Protecttion";
        } else {
            return "CheckingAccount with a " + getBalance() + " balance and no overdraf Protecttion";
        }

    }

    public String toString2() {
        return "Checking Acccount :current balance is $ " + balance;
    }

    @Override
    public void withdraw(double amt) throws OverDraftException {
//        return super.withdraw(amt);
        if (amt <= balance && amt > 0) {
            balance -= amt;

        } else if (amt > balance) {
            if (amt < (balance + overdrafProtecttion)) {

                overdrafProtecttion -= (amt - balance);
                balance = 0;

            } else if (overdrafProtecttion == 0) {
                throw new OverDraftException("no over pro", amt - balance);
            } //                throw  new OverDraftException("Inffucient funds for ", (amt-(balance+overdrafProtecttion)));
            else if (amt > (balance + overdrafProtecttion)) {
                throw new OverDraftException("inffu", (amt - (balance + overdrafProtecttion)));
            }

        }
    }

    public void rut(CheckingAccount acc, double amt) {
        try {
            acc.withdraw(amt);
        } catch (OverDraftException e) {
            System.out.println("Exception:" + e.getMessage());
            System.out.println("Deficit:" + e.getDeficit());
        }
    }
}
